package gray.boolber.vk.web.api.account;

import android.content.Context;
import android.content.SharedPreferences;

public class RuntimePreferences {
	private static final String mName = "VKRuntimePreferences";
	private static RuntimePreferences mSpUtils;
	private SharedPreferences mSp;
	public static RuntimePreferences from(Context context)
	{
		mSpUtils = new RuntimePreferences(context);
		return mSpUtils;
	}
	
	private RuntimePreferences(Context context) {
		mSp = context.getSharedPreferences(mName, 0);
	}
	//settings
	//***********************************************************************************************************************************************
	public void saveVKAccessToken(String access_token){
		SharedPreferences.Editor editor = mSp.edit();
		editor.putString("VK_ACCESS_TOKEN", access_token);
		editor.commit();
	}
	public void saveVKExpiredIn(String pref){
		SharedPreferences.Editor editor = mSp.edit();
		editor.putString("VK_EXPIRED_IN", pref);
		editor.commit();
	}
	public void saveVKUserId(String pref){
		SharedPreferences.Editor editor = mSp.edit();
		editor.putString("VK_USER_ID", pref);
		editor.commit();
	}
	public void saveVKAccessToken(String[] pref){
		SharedPreferences.Editor editor = mSp.edit();
		editor.putString("VK_ACCESS_TOKEN", pref[0]);
		editor.putString("VK_EXPIRED_IN", pref[1]);
		editor.putString("VK_USER_ID", pref[2]);
		editor.putLong("VK_ACCESS_TIME", System.currentTimeMillis());
		editor.commit();
	}
	public String[] getVKSessionToken(){
    	String[] params = new String[4];
    	params[0] = mSp.getString("VK_ACCESS_TOKEN", "");
    	params[1] = mSp.getString("VK_EXPIRED_IN", "");
    	params[2] = mSp.getString("VK_USER_ID", "");
    	params[3] =  String.valueOf(mSp.getLong("VK_ACCESS_TIME",86400));
    	return params;
    }
	public String getVKAccessToken(){
		return mSp.getString("VK_ACCESS_TOKEN", "");
	}
	public String getVKExpiredIn(){
		return mSp.getString("VK_EXPIRED_IN", "86400");
	}
	public String getVKUserId(){
		return mSp.getString("VK_USER_ID", "");
	}
	public long getVKAccessTime(){
		return mSp.getLong("VK_ACCESS_TIME", 0l);
	}
	public boolean isAccessTokenValid(){
		if(System.currentTimeMillis()<(getVKAccessTime()+(86400*1000l))){
			return true;
		}else{
			return false;
		}
	}

}
